<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@include file="/WEB-INF/jsp/common/taglibs.jsp" %>
<html>
<head>
    <title>网盘上传文件</title>
    <%@include file="/WEB-INF/jsp/common/meta2.jsp" %>
    <script src="${ctx}/static/plugin/file.util/util.js"></script>
    <link rel="stylesheet" href="${ctx}/static/plugin/file.util/style.css" type="text/css"/>

</head>
<body class="content-body">
<div class="container-fluid">

    <table class="table">
        <tr>
            <th colspan="20" class="bg-info text-center">
                文件上传
            </th>
        </tr>
        <tr>
            <td>
                <form id="inputForm" action="${ctx}/platform/skyDrive/save" method="post">
                    <input name="id" id="id" value="${entity.id}" type="hidden"/>
                    <input name="size" id="size" value="${entity.size}" type="hidden"/>
                    <input name="userId" id="userId" value="${entity.userId == null ? user.id:entity.userId}"
                           type="hidden"/>
                    <table class="table">
                        <tbody>

                        <tr>
                            <td></td>
                            <td>
                                <p class="text-info">

                                    亲，你的网盘总容量
                                    <fmt:formatNumber value=" ${user.skyDriveSize/1024/1024}" pattern="##.##"
                                                      minFractionDigits="2"></fmt:formatNumber> MB
                                    ，已使用
                                    <fmt:formatNumber value="${user.usedSize/1024/1024}" pattern="##.##"
                                                      minFractionDigits="2"></fmt:formatNumber> MB，
                                    还剩<span class="text-red"><fmt:formatNumber
                                        value="${(user.skyDriveSize - user.usedSize)/1024/1024}"
                                        pattern="##.##"
                                        minFractionDigits="2"></fmt:formatNumber> MB</span>可用
                                </p>
                            </td>
                        </tr>
                        <tr>
                            <td>文件：</td>
                            <td>
                                <input class="input_file_02" type="text" id="field" name="name">
                                <input class="input_file_02" readonly type="hidden" value="${entity.fileId}" id="fileId"
                                       name="fileId">
                                <input class="btn_db show_dialog" type="button" onclick="uploadZipFile()" value="上传文件">
                                <span id="progress"></span>
                <span class="text-info">
                    目前仅支持zip|rar|doc|xls|docx|xlsx|png|jpg|jpeg|txt|apk|pdf|ppt|pptx|dmg格式文件上传！
                </span>
                            </td>
                        </tr>
                        <tr>
                            <td>描述：</td>
                            <td>
                                <textarea class="form-control" style="width: 70%;" rows="8" name="memo">${entity.memo}</textarea>
                            </td>
                        </tr>
                        <tr>
                            <td></td>
                            <td>
                                <input type="submit" value="确定" class="btn_db show_dialog">
                                <input class="btn_red" onclick="history.go(-1);" type="button" value="返回"/>
                            </td>
                        </tr>
                        </tbody>
                    </table>
                </form>

                <iframe src="${ctx}/common/file/upload" class="hidden"></iframe>
            </td>
        </tr>
    </table>
</div>
</body>
</html>

<script>
    var leftSkyDriveFileSize = "${user.skyDriveSize - user.usedSize}";
    leftSkyDriveFileSize = parseFloat(leftSkyDriveFileSize);
    $(function () {
        $("#inputForm").validate({
            rules: {
                name: {
                    required: true
                }
            },
            errorPlacement: function (error, element) {
                error.appendTo($(element).parent());
            },
            messages: {
                name: {
                }
            }
        });
    });

    $(function () {
        var FileData = {
            file: $("#field"),
            button: $(".btn_db:button"),
            target: $("#progress"),
            id: 0,
            size: $("#size"),
            attachment: $("#fileId")
        }

        if (FileData.attachment.val()) {
            $.get(window.BasePath + "/common/file/info/" + FileData.attachment.val(), function (fileObject) {
                if (fileObject) {
                    FileData.file.val(fileObject.original);
                    FileData.target.html((fileObject.size / 1024).toFixed(2) + "KB.");
                    FileData.size.val(fileObject.size);
                }
            }, "json");
        }

        window.uploadZipFile = function () {
            var $progress = FileData.target;
            $progress.html("...").attr("loading", 1);
            top.setUploadFileData = function (fileObject) {
                FileData.id = fileObject.id;
                FileData.file.val(fileObject.original);
                FileData.attachment.val(fileObject.id);
                FileData.size.val(fileObject.size);
//                $scope.formData.size = fileObject.size;
                $progress.attr("loading", 0);
                $progress.html("文件大小：" + (fileObject.size / 1024).toFixed(2) + "KB.");
            }
            if (top.window.UploadFileUtil) {
                top.window.UploadFileUtil.checkFileSize = function ($fileInput) {
                    var _self = this;
                    var fileSize0 = _self.getFileSize($fileInput[0]);
                    if (fileSize0 > leftSkyDriveFileSize) {
                        alert("上传的文件(" + (fileSize0 / 1024 / 1024).toFixed(2) + "MB." + ")大小已经超过网盘可用容量，请清理部分文件后重试！")
                        return false;
                    } else {
                        return true;
                    }
                }
                var reg = /.(?:zip|rar|doc|xls|docx|xlsx|png|jpg|jpeg|txt|apk|pdf|ppt|pptx|dmg)$/;
                top.window.UploadFileUtil.selectFile(reg, "目前仅支持zip|rar|doc|xls|docx|xlsx|png|jpg|jpeg|txt|apk|pdf|ppt|pptx|dmg格式文件上传！", $progress);
            }
        }

//        window.submitCourseData = function () {
//            $("#")
//        }
    });


</script>
<c:if test="${(user.skyDriveSize - user.usedSize) <= 0 }">
    <script>
        alert('亲，你的网盘总容量  <fmt:formatNumber value=" ${user.skyDriveSize/1024/1024}" pattern="##.##"  minFractionDigits="2"></fmt:formatNumber> MB，已使用<fmt:formatNumber value="${user.usedSize/1024/1024}" pattern="##.##" minFractionDigits="2"></fmt:formatNumber> MB，还剩 <fmt:formatNumber value="${(user.skyDriveSize - user.usedSize)/1024/1024}"  pattern="##.##" minFractionDigits="2"></fmt:formatNumber> MB可用；网盘容量超限，请清理部分文件后重试！');
        history.go(-1);
    </script>
</c:if>